/**
 * 
 */
package LogViewer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.Timer;

import org.pexx.components.logger.Log;
import org.pexx.rdm.config.RiftConfig;
import org.pexx.rdm.data.Collector;
import org.pexx.rdm.database.Database;
import org.pexx.rdm.plugin.Plugin;


/**
 * 
 */
public class LogViewer extends Plugin {

	private Timer timer;
	private LogViewerPanel panel;
	
	
	/**
	 * @param collector
	 * @param database
	 * @param config
	 */
	public LogViewer(Collector collector, Database database, RiftConfig config) {

		super(collector, database, config);

		initGui();
		initTimer();
	}

	/**
	 * 
	 */
	private void initTimer() {

		timer = new Timer(1000, new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
			
				updateTextfield();
				
			}
		});

		updateTextfield();
		timer.start();
		
	}

	private void updateTextfield() {

		String l = Log.getLog().toString();
		
		if (panel.getLog().getText().equals(l))
			return;
		
		panel.getLog().setText(l);
		panel.getLog().setCaretPosition(l.length());		
		
	}

	/**
	 * 
	 */
	private void initGui() {

		panel = new LogViewerPanel();
		
	}

	/* (non-Javadoc)
	 * @see org.pexx.rdm.plugin.Plugin#getComponentPanel()
	 */
	@Override
	public JPanel getComponentPanel() {
		return panel;
	}

	/* (non-Javadoc)
	 * @see org.pexx.rdm.plugin.Plugin#dispose()
	 */
	@Override
	public void dispose() {

		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see org.pexx.rdm.plugin.Plugin#reset()
	 */
	@Override
	public void reset() {

		// TODO Auto-generated method stub
		
	}

	
}
